package com.hnkywl.jip.action;

import com.hnkywl.jip.interceptor.AuthInterceptor;
import com.hnkywl.jip.service.LoginService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-3-11
 * Time: 上午10:49
 * To change this template use File | Settings | File Templates.
 */
@ParentPackage("default")
@Namespace("/main")
public class LoginAction extends SimpleActionSupport {
    private String userName;

    private String password;

    private LoginService loginService;


    @Action("login")
    public String login() {
        if (this.isPost()) {
            if (StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(password)) {
                Object[] user = this.loginService.login(userName, password);
                if (user != null) {
                    Map temp = new HashMap<String, Object>();
                    temp.put(AuthInterceptor.USER_ID_SESSION_KEY, user[0]);
                    temp.put(AuthInterceptor.USER_NAME_SESSION_KEY, user[1]);
                    temp.put(AuthInterceptor.USER_PASSWORD_SESSION_KEY, password);
                    temp.put(AuthInterceptor.JIP_USERNAME_SESSION_KEY, user[2]);
                    temp.put(AuthInterceptor.JIP_PASSWORD_SESSION_KEY, user[3]);
                    this.session.put(AuthInterceptor.SESSION_KEY, temp);
                    this.renderText("true");
                    return null;
                } else {
                    this.renderText("用户名或者密码输入错误！");
                    return null;
                }
            }
            this.renderText("用户名或者密码不能为空！");
            return null;
        } else
            return SUCCESS;
    }

    @Action("logout")
    public String logout() {
        if (session != null)
            session.put(AuthInterceptor.SESSION_KEY, null);
        return "login";
    }


    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Autowired
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }
}
